﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WarriorRush.GameDynamics;
using WarriorRush.GameDynamics.Characters;
using WarriorRush.GameDynamics.Utilities;
using Microsoft.Xna.Framework;

namespace WarriorRush.UserInterface
{
    public class SetupManager
    {
        public SelectionHandler SelectionOption;
        public Player CurrentPlayer;
        public int CurrentRow;
        public Arena TheArena;
        public WarriorCodex Inventories;
        private ArenaSquare CurrentSquare;
        public Dictionary<CharacterTypes, Rectangle> CharacterOptions;
       

        public SetupManager(Arena theArena, WarriorCodex inventories)
        {
            TheArena = theArena;
            Inventories = inventories;
            StartNewSetup();
            var warriorChooser = new Random();
            foreach (var row in TheArena.Squares)
            {
                foreach (var square in row)
                {
                    square.Character = inventories.GetCharacter((CharacterTypes) warriorChooser.Next(0,(int)CharacterTypes.Null));
                }
            }
            CurrentPlayer = Player.one;
            CharacterOptions = new Dictionary<CharacterTypes, Rectangle>();
            
            var charIndex = 0;
            foreach (var characterType in Inventories.CharacterLookup)
            {
                CharacterOptions.Add(characterType.Key, new Rectangle(300 +100 * charIndex , 250, 160 , 160 ));
                charIndex ++;
            }

        }

        public void StartNewSetup()
        {
            CurrentRow = 1;
            CurrentPlayer = Player.one;

        }

        public void SelectSquare(ArenaSquare square)
        {
            CurrentSquare = square;
        }

        public bool CommitSelection()
        {
            if (CurrentRow != 3)
                return false;
            CurrentPlayer = CurrentPlayer == Player.one ? Player.two : Player.one;
            switch (CurrentRow)
            {
                case 1: CurrentRow = 2; break;
                case 2: CurrentRow = 0; break;
                case 0: CurrentRow = 3; break;
            }
            return true;
        }

        public void MakeSelection(CharacterClass character, ArenaSquare square)
        {
            square.Character = character;

        }
        public SelectionHandler GetCurrentSquares()
        {
            var selections = new SelectionHandler();
            selections.MaxSelections = 1;
            selections.IsReplacedSelection = true;
            selections.Squares = TheArena.Squares[(int)CurrentPlayer * 2 ].Select(f => new Selectable { Square = f }).ToList();
            selections.Squares.AddRange(TheArena.Squares[(int)CurrentPlayer * 2 + 1].Select(f => new Selectable { Square = f }));
            return selections;
        }

        public SelectionHandler GetAttackRows(Player player)
        {
            var selections = new SelectionHandler();
            selections.MaxSelections = 1;
            selections.IsReplacedSelection = true;
            selections.Squares = TheArena.Squares[(int)player + 1].Where(f=>f.Character!=null).Select(f => new Selectable { Square = f }).ToList();
            return selections;
        }
    }
}
